var request = window.indexedDB.open('game-db', 1);
request.onupgradeneeded = function(event) {
	var db = event.target.result;
	var objectStore = db.createObjectStore('game-score', {
		name: 'scores',
		keyPath: 'id',
		autoIncrement: true,
		indexes: [{
			name: 'score',
			unique: false
		}]
	});
};
var score = 100;
var transaction = db.transaction(['game-score'], 'readwrite');
var objectStore = transaction.objectStore('game-score');
var request = objectStore.add({
	id: 1,
	score: score
});

function saveScore(score) {
	let transaction = db.transaction([scoreStore.name], 'readwrite');
	let store = transaction.objectStore(scoreStore.name);
	let request = store.add({
		score: score
	});

	request.onerror = function() {
		console.log('Error saving score to database.');
	};

	transaction.oncomplete = function() {
		console.log('Score saved to database.');
	};
}

function getScores(callback) {
	let transaction = db.transaction([scoreStore.name], 'readonly');
	let store = transaction.objectStore(scoreStore.name);
	let scores = [];

	let request = store.openCursor();

	request.onsuccess = function(event) {
		let cursor = event.target.result;
		if (cursor) {
			scores.push(cursor.value.score);
			cursor.continue();
		} else {
			callback(scores);
		}
	};

	request.onerror = function() {
		console.log('Error retrieving scores from database.');
	};
}